package edu.whut.mall.admin.controller;

import edu.whut.mall.admin.domain.GroupBuyRuleParams;
import edu.whut.mall.admin.domain.ProductParams;
import edu.whut.mall.admin.domain.ProductSearchParams;
import edu.whut.mall.admin.service.IProductService;
import edu.whut.mall.admin.vo.ProductVo;
import edu.whut.mall.common.api.CommonPage;
import edu.whut.mall.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * @program: d9lab-mall
 * @description: 商品控制器
 * @author: Wayne
 * @create: 2020-05-05 15:54
 **/
@Slf4j
@Api(value="商品控制器",tags = {"商品控制器"})
@RestController
@RequiredArgsConstructor
@RequestMapping("/product")
public class RestProductController {
    private final IProductService productService;

    @ApiOperation("添加商品")
    @PostMapping(value="/addOneProduct")
    public CommonResult addOneProduct(@Valid @RequestBody ProductParams productParams){
        int result = productService.insertOneProduct(productParams);
        if (result >= 0) {
            return CommonResult.success("添加商品成功");
        }
        return CommonResult.failed("商品添加失败");
    }

    @ApiOperation("更新商品")
    @PostMapping(value="/updateOneProduct")
    public CommonResult updateOneProduct(@NotNull @RequestParam Long productId,
                                         @Valid @RequestBody ProductParams productParams){
        int result = productService.updateOneProduct(productId, productParams);
        if (result >= 0 ) {
            return CommonResult.success("更新商品成功");
        }
        return CommonResult.failed("更新商品失败");
    }

    @ApiOperation("查询商品详情")
    @GetMapping(value="/getProductDetailVoByProductId")
    public CommonResult getProductDetailVoByProductId(@NotNull @RequestParam Long productId){
        ProductVo productVo = productService.getProductDetailVoByProductId(productId);
        if (productVo != null) {
            return CommonResult.success(productVo,"获取商品详情成功");
        }
        return CommonResult.failed("获取商品详情失败");
    }

    @ApiOperation("根据商品分类id获取商品页面")
    @GetMapping(value="/listProductVosByCategoryId")
    public CommonResult listProductVosByCategoryId(@NotNull @RequestParam Long categoryId,
                                                   @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value="size",defaultValue ="10") Integer size){
        CommonPage<ProductVo> productVoPage = productService.listProductVosByCategoryId(categoryId, pageNum, size);
        if (productVoPage != null) {
            return CommonResult.success(productVoPage,"获取商品页面成功");
        }
        return CommonResult.failed("获取商品页面失败");
    }

    @ApiOperation("商家获取自家全部商品")
    @GetMapping(value="/listMerchantOwnProductVos")
    public CommonResult listMerchantOwnProductVos(@RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value="size",defaultValue ="10") Integer size){
        CommonPage<ProductVo> productVoPage = productService.listMerchantOwnProductVos(pageNum, size);
        if (productVoPage != null) {
            return CommonResult.success(productVoPage,"获取商品页面成功");
        }
        return CommonResult.failed("获取商品列表失败");
    }

    @ApiOperation("根据条件获取商品列表")
    @PostMapping(value = "listProductVosByCondition")
    public CommonResult listProductVosByCondition(@RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value="size",defaultValue ="10") Integer size,
                                                  @RequestBody ProductSearchParams productSearchParams) {
        CommonPage<ProductVo> productVoPage = productService.listProductVosByCondition(productSearchParams, pageNum, size);
        if (productVoPage != null) {
            return CommonResult.success(productVoPage, "获取商品页面页面成功");
        }
        return CommonResult.failed("获取商品列表失败");
    }

    @ApiOperation("更新商品状态")
    @PostMapping(value="/updateOneProductStatus")
    public CommonResult updateOneProductStatus(@NotNull @RequestParam Long productId, @NotNull @RequestParam Integer status){
        int result = productService.setProductStatus(productId, status);
        if (result >=0 ) {
            return CommonResult.success("更新商品状态成功");
        }
        return CommonResult.failed("更新商品状态失败");
    }

    @ApiOperation("更新商品审核状态")
    @PostMapping(value = "/updateOneProductVerifyStatus")
    public CommonResult updateOneProductVerifyStatus(@NotNull @RequestParam Long productId,
                                                     @NotNull @RequestParam Integer status) {
        int result = productService.setProductVerifyStatus(productId, status);
        if (result >= 0) {
            return CommonResult.success("更新商品审核状态成功");
        }
        return CommonResult.failed("更新商品审核状态失败");
    }

    @ApiOperation("删除商品")
    @PostMapping(value="/deleteOneProduct")
    public CommonResult deleteOneProduct(@NotNull @RequestParam Long productId){
        int result = productService.deleteOneProduct(productId);
        if (result >=0 ) {
            return CommonResult.success("删除商品成功");
        }
        return CommonResult.failed("删除商品失败");
    }

    @ApiOperation("为商品绑定团购规则")
    @PostMapping(value = "assignGroupBuyRuleToProduct")
    public CommonResult assignGroupBuyRuleToProduct(@NotNull @RequestBody GroupBuyRuleParams groupBuyRuleParams) {
        int result = productService.assignGroupBuyRuleToProduct(groupBuyRuleParams);
        if (result >=0 ) {
            return CommonResult.success("为商品绑定规则成功");
        }
        return CommonResult.failed("为商品绑定规则失败");
    }
}
